<?php

namespace App\Logic\reservation;

use App\Models\Order;
use Illuminate\Support\Str;
use App\Jobs\QueueAuth;
use App\Models\Login;
use App\Models\Patient;
use App\Server\ApiService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Matrix\Exception;
use Illuminate\Support\Facades\Mail;
class ReservAtion
{
    /**
     * @预约
     * @user wsg
     * @method post
     * @data
     * @return \Illuminate\Http\Response
     */
    public static function reservAtion($data)
    {
        //接受过来的参数
        $queue = new QueueAuth($data);
        //连接redis
        $redis = new \Redis();
        $redis->connect('127.0.0.1', '6379');
        //得到用户id
        $vid = 1;
        if (empty($vid)) {
            return ApiService::fail('参数异常');
        }
        //查询用户
        $userInfo = Login::where('id', $vid)->first();
        if (empty($userInfo)) throw new Exception('参数错误');

        $reservation=[
            'patient' => '余淮',
            'visit_card' => '65251254215',
            'doctor_id' => '1',
            'id_card' => '152463521521252152',
            'phone' => '15826351254',
            'user_id' => '1',
            'sub_office' => '心脏内科',
            'sub_type' => '1',
            'sub_price' => '60.50',
            'sub_status' => 0,
            'address' => '布达拉宫',
            'img' => '/storage/img/WfMaG4IUGtRhlqphuswJlNGSiCaAeOtgaT2lWo18.jpg',
            'sub_time' => date('Y-m-d H:i:s'),
            'visit_time' => date('Y-m-d H:i:s'),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        //添加预约数据
        $patient = Patient::create($reservation);

        //创建订单
        $orderData = [
            'order_no' => Str::random(20),
            'user_id' => 11,
            'activity_id' => 1,
            'sckill_goods_id' => 5,
            'original_price' => '60.00',
            'total_price' => '60.00',
            'status' => 0,
            'pay_method' => 0,
            'address' => json_encode([
                'name' =>'张先生',
                'home' => '上海市奉贤区',
            ]),
            'num' => 1,
            'ramake' => null,
            'trade_no' => null,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        Order::create($orderData);
//        DB::table('order')->insert($orderData);
        try {
            //开启事务
            DB::beginTransaction();
//            Mail::raw("《《《《恭喜你预约成功，请及时查看》》》》",function ($mesage){
//                $mesage->from("2234736237@qq.com","王帅国");
//                $mesage->subject("《《《《恭喜你预约成功，请及时》》》》");
//                $mesage->to("2234736237@qq.com");
//            });
            //运行消息队列     php artisan queue:work rabbitmq --queue=test
            //将预约结果写到日志里
            //将任务推送到队列            指定消息队列的连接名称          指定任务队列的名称
            QueueAuth::dispatch($queue)->onConnection('rabbitmq')->onQueue('test');

            DB::rollBack();
            return $patient;
        }catch (Exception $exception){
            //结束事务
            DB::enableQueryLog();
            Log::info($exception->getMessage());
        }
    }
}
